package com.woniuxy.ticket.ticketmovie.controller;

import com.woniuxy.ticket.ticketmovie.entity.ResponseResult;
import com.woniuxy.ticket.ticketmovie.entity.Type;
import com.woniuxy.ticket.ticketmovie.groups.TypeUpdateGroup;
import com.woniuxy.ticket.ticketmovie.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;

@Validated
@Api(tags = "类型管理接口")
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @ApiOperation("查询所有电影类型")
    @GetMapping("/findAllType")
    public ResponseResult<List<Type>> findAllType(){
        return new ResponseResult<List<Type>>().success(typeService.list());
    }

    @ApiOperation("新增电影类型")
    @PostMapping("/add")
    public ResponseResult add(@RequestBody @Valid Type type){
        typeService.add(type);
        return new ResponseResult<>().success();
    }

    @ApiOperation("根据类型id查询类型")
    @GetMapping("/findById/{id}")
    public ResponseResult<Type> findById(@PathVariable @Min(value = 1,message = "id无效") int id){
        return new ResponseResult<Type>().success(typeService.getById(id));
    }

    @ApiOperation("修改类型")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Validated(TypeUpdateGroup.class) Type type){
        typeService.updateType(type);
        return new ResponseResult<>().success();
    }

    @ApiOperation("查询所有类型，不分页")
    @GetMapping("/list")
    public ResponseResult<List<Type>> list(){
        return new ResponseResult<List<Type>>().success(typeService.list());
    }
}
